package com.yoo.ons.dialog

import android.app.Activity
import android.graphics.Point
import android.graphics.Rect
import android.os.Handler
import android.view.Display
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.FragmentActivity
import com.gyf.immersionbar.ImmersionBar
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.module_base.utils.setSystemBottomNavBarColor
import com.yoo.module_base.utils.setSystemBottomNavBarNoColor
import com.yoo.ons.R
import com.yoo.ons.ToastUtil
import razerdp.basepopup.BasePopupWindow

class UpdateAboutMePopup(activity: FragmentActivity, var aboutMe:String, var callBack:(name:String)->Unit):BasePopupWindow(activity) {
    init {
        setContentView(R.layout.popup_update_about_me)
        initView(activity)
        showAnimation=  AnimationUtils.loadAnimation(activity, com.yoo.module_res.R.anim.anims_slide_in_bottom)
        dismissAnimation=  AnimationUtils.loadAnimation(activity, com.yoo.module_res.R.anim.anims_slide_out_bottom)
    }

    fun initView(activity:FragmentActivity){
        val outView = findViewById<ConstraintLayout>(R.id.out_view)
        val popupClose = findViewById<ImageView>(R.id.popup_close)
        val popupEditAboutMe = findViewById<EditText>(R.id.popup_edit_about_me)
        val popupUpdateAboutMeSave = findViewById<TextView>(R.id.popup_update_about_me_save)
        popupClose.setOnClickCallback {
            dismiss()
        }
        popupEditAboutMe.setText(aboutMe)
        popupEditAboutMe.setSelection(aboutMe.length)

        popupUpdateAboutMeSave.setOnClickCallback {
            if (popupEditAboutMe.text.toString().trim().isNullOrEmpty()){
                ToastUtil.showToast("input cannot be empty")
                return@setOnClickCallback
            }
            callBack.invoke(popupEditAboutMe.text.toString())
            dismiss()
        }



        //设置键盘
        val decorView: View = activity.window.decorView
        decorView.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            activity.window.decorView.getWindowVisibleDisplayFrame(r)
            val defaultDisplay: Display = activity.windowManager.defaultDisplay
            val point = Point()
            defaultDisplay.getSize(point)
            val height = point.y
            val heightDifference = height - (r.bottom - r.top) // 实际高度减去可视图高度即是键盘高度
            outView.setPadding(0,0,0,heightDifference)
        }

        //设置底部导航栏颜色
        setSystemBottomNavBarColor(activity)
        onDismissListener =object : OnDismissListener() {
            override fun onDismiss() {
                setSystemBottomNavBarNoColor(activity)
            }

        }
    }
}